import axios from 'axios'
import {
    Message,
    MessageBox
} from 'element-ui'
import {
    setToken,
    getToken
} from '../assets/js/utils'
import baseUrl from '../assets/js/requestAddress'
const debug = process.env.NODE_ENV !== 'production'

// create an axios instance
const service = axios.create({
    withCredentials: false,
    timeout: 10000, // 请求超时10秒
    baseURL: process.env.BASE_API
})

// request interceptor
service.interceptors.request.use(
    config => {
        //规定各个API请求开头
        const phone = "/phone";
        const broadband = "/broadband";
        const traffic = "/traffic";
        const user = "/user";
        const order = "/order";
        const customer = "/customer";
        const admin = "/admin";

        // 根据不同的调用名 去到不同的服务端口
        // if (config.url.indexOf(phone) === 0) {
        //     config.url = baseUrl.PHONE_URL + config.url;
        // } else if ( config.url.indexOf(broadband) === 0) {
        //     config.url = baseUrl.BROADBAND_URL + config.url;
        // } else if (config.url.indexOf(traffic) === 0) {
        //     config.url = baseUrl.TRAFFIC_URL + config.url;
        // } else if (config.url.indexOf(user) === 0) {
        //     config.url = baseUrl.USER_URL + config.url;
        // } else if (config.url.indexOf(order) === 0) {
        //     config.url = baseUrl.ORDER_URL + config.url;
        // } else if (config.url.indexOf(customer) === 0) {
        //     config.url = baseUrl.CUSTOMER_URL + config.url;
        // } else if (config.url.indexOf(admin) === 0) {
        //     config.url = baseUrl.ADMIN_URL + config.url;
        // }

        //如果token为空 说明第一次登录本系统 则不管
        //如果token不为空 则带着这个给header
        if (getToken("token") !== null) {
            config.headers['token'] = getToken("token")
        }
        if (debug) {
            // console.log(config)
        }
        return config
    },
    error => {
        // Do something with request error
        console.log(error) // for debug
        Promise.reject(error)
    }
)

//若第一个异步请求报错 后面的都暂停
let flag = false;

// response interceptor
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code === 10010 && flag === false) {
            flag = true;
            setToken("token", null);
            setToken("userInfo", null);
            MessageBox.confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                window.location.href = "/"
            }).catch(() => {
                window.location.href = "/"
            })
        }
        // 请求成功
        else {
            return response.data
        }
    },
    error => {
        if (flag === true) return;
        Message({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        flag = true;
        return Promise.reject(error)
    }
)

export default service